package com.yjht.online.arg.reslove.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import com.yjht.online.arg.ArgParam;
import com.yjht.online.arg.excption.ArgError;
import com.yjht.online.arg.reslove.ArgResloveByKey;
import com.yjht.online.generate.impl.org.OrgGenerate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

public class SysParamReslove implements ArgResloveByKey {

    final static Logger logger = LoggerFactory.getLogger(SysParamReslove.class);

    @Override
    public boolean support(String key) {


        return Arrays
                .asList("isDev", "keyId", "corpCd", "priKey", "dqdh", "jgdh",
                        "jygy", "accessKeyId", "accessKeySecret", "kfPhon", "secretId",
                        "secretKey", "signName", "templateCode", "sgbNumber", "unitNumber").contains(key);
    }

    @Override
    public void resolve(ArgParam argParam, String key, List<String> value) throws ArgError {
        if (CollectionUtil.isEmpty(value)) {
            throw new ArgError("参数错误");
        }
        if (value.size() > 1) {
            throw new ArgError("参数错误");
        }

        Field field = ReflectUtil.getField(argParam.getClass(), key);
        if (field == null) {
            logger.error("系统参数名需与实体类字段一样" + key);
            return;
        }
        ReflectUtil.setFieldValue(argParam,field, value.get(0));
    }


}
